package com.zyguo.thread;

import java.util.ArrayList;


public class Main_lock {
	public static void main(String[] args) {
		final UserLock u = new UserLock(1);
		int threadNum = 3;
		final ThreadGroup tgroup = new ThreadGroup("test-threadgroup");
		final ArrayList<Thread> tList = new ArrayList<>();
		//定义10个线程
		for( int i = 0; i < threadNum; i++ ){
			Thread t = new Thread( tgroup, new Runnable() {
				@Override
				public void run() {
					u.addMoney(1);
				}
			} ,"test-thread-" + i);
			
			tList.add( t );
			t.start();
			System.out.println("start thread = " + t );
		}

		//监控线程的活动的子线程数
		Thread t = new Thread( new Runnable() {
			@Override
			public void run() {
				int activeCount = tgroup.activeCount();
				while ( activeCount > 0 ) {
					for (Thread thread : tList) {
						System.out.println( thread + ",state=" + thread.getState() );
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					activeCount = tgroup.activeCount();
				}
			}
		});
		
		t.start();
		
	}
	
}

